package com.kasax.imgloader.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kasax.imgloader.demo.R;
import com.kasax.imgloader.demo.bean.SubListInfo;

import java.util.List;

public class HomeListAdapter extends RecyclerView.Adapter<MyViewHodler> {
    private Context context;
    private LayoutInflater layoutInflator;
    private List<SubListInfo.DataBean> dataList;

    public HomeListAdapter(Context context, List<SubListInfo.DataBean> list) {
        this.context = context;
        this.layoutInflator = LayoutInflater.from(context);
        this.dataList = list;
    }

    public void updateList(List<SubListInfo.DataBean> list) {
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflator.inflate(R.layout.layout_sub_item, parent, false);
        return new MyViewHodler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHodler holder, int position) {
        SubListInfo.DataBean dataBean = dataList.get(position);
        holder.setAuthor(dataBean.getAuthor());
        holder.setDesc(dataBean.getDesc());
        holder.setLisense(dataBean.getLisense());
        holder.setName(dataBean.getName());
        if (position % 2 == 0) {
            holder.setImage(dataBean.getCover());
        } else {
            holder.setImageLoader(dataBean.getCover());
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


}
